
import { getStorage } from '@/utils/tools'
import { MethodType, Result } from "@/types";
import { routeRedirectTo } from '@/utils/route'


// #ifdef MP-WEIXIN
const app_type = 'weapp';
const app_type_name = '微信小程序';
// #endif

export const request = (options: {
    url: string;
    method?: MethodType;
    data?: any;
    header?: object;
    dataType?: string;
    responseType?: string;
    [key: string]: string | any | unknown;
}, _type: string = 'default') => {
    var method: MethodType = options.method || 'GET', // 请求方式
        url = import.meta.env.VITE_BASE_URL + 'api/' + options.url, // 请求路径
        data = {
            app_type,
            app_type_name
        };
    if (options.method != undefined) method = options.method;

    // 参数
    if (options.data != undefined) Object.assign(data, options.data);
    var header: {
        token?: string;
        'Content-Type'?: string;
        'Authori-zation'?: string;
    } = {}
    if (!options.header) {
        header['Content-Type'] = 'application/json';
    }
    // token
    if (getStorage('token')) {
        header['Authori-zation'] = 'Bearer ' + getStorage('token')
    }
    if (_type == 'upload') {
        return new Promise<Result>((resolve, reject) => {
            if (!options.filePath) {
                reject({
                    data: '上传文件路径不能为空',
                    code: 'ERRPR'
                });
            }
            uni.uploadFile({
                url: url,
                filePath: options?.filePath,
                name: options?.fileName || 'image',
                header: header,
                formData: data,
                success: (res: any) => {
                    var resData = JSON.parse(res.data);
                    if (resData?.code === 410000) {
                        routeRedirectTo('/pages/login/index');
                    }
                    if (resData.code === 410000) {
                        reject({
                            data: resData,
                            code: 'ERRPR_AUTH'
                        });
                    } else if (resData.code !== 1) {
                        reject({
                            data: resData,
                            code: 'ERRPR'
                        });
                    }
                    resolve(resData);
                },
                fail: (failRes) => {
                    reject({
                        data: failRes.errMsg,
                        code: 'SERVER_ERRPR'
                    })
                }
            });

        });
    } else {
        return new Promise<Result>((resolve, reject) => {
            uni.request({
                url: url,
                method: method,
                data: data,
                header: header,
                dataType: options.dataType || 'json',
                responseType: options.responseType || 'text',
                success: (res: any) => {
                    if (res?.data?.code === 410000) {
                        routeRedirectTo('/pages/login/index');
                    }
                    if (res.data.code === 410000) {
                        reject({
                            data: res.data,
                            code: 'ERRPR_AUTH'
                        });
                    } else if (res.data.code !== 1) {
                        reject({
                            data: res.data,
                            code: 'ERRPR'
                        });
                    }
                    resolve(res.data);
                },
                fail: (failRes) => {
                    reject({
                        data: failRes.errMsg,
                        code: 'SERVER_ERRPR'
                    })
                }
            });
        });
    }


}


export default request